#include "sql_ctl.h"
#include "sqlite3.h"
#include "ddebug.h"
#include "recvmsg_type.h"
//此宏作废，千万不要定义此宏，否则无法正常工作
#ifndef GOODCHARSIMPLE
/*函数功能：操作数据库，添加或者减少商品数量
 *参数：sto-仓库号，goodsnum-RFID卡所携带的ID,goodsvalue-商品缩写的Data，cmd-操作数,1为入库，0为出库
 *1:检查数据库中该仓库是否有该商品，如果有则进行增减操作，如果没有则增加商品信息
 *2:
 *---如果没有该商品，先看仓库中有多少商品，再给当前商品编号，插入商品
 *---如果有该商品，如果是入库，直接把goods_count加一存入，如果是出库，判断该该商品的数量是否为零
 *----若是零，则直接返回，否则把goods_count减一保存
 */
int sql_ctl_goods(unsigned char sto,int goodsnum, unsigned char *goodsvalue, int cmd)
{
	DDEBUG(P_NORMAL,"<<SQLIST>> in sql_ctl 1 %d, %d,%s,%d\n",sto,goodsnum,goodsvalue,cmd);
	sqlite3 *db;
	if(sqlite3_open("/www/cgi-bin/warehouse.db",&db) != SQLITE_OK)
	{
		DDEBUG(P_URGENCY,"<<SQLIST>> error:%s\n",sqlite3_errmsg(db));
		return -1;
	}
	char buf[100] = {0};

	sprintf(buf, "select goods_count from goods where dev_no = '%d' and goodsvalue = \"%s\"", sto, goodsvalue);
	DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);
	char *errmsg, **resultp;
	int nrow, ncolumn/*, i,j,index*/;

	if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
		return -2;
	}
	DDEBUG(P_URGENCY,"<<SQLIST>> row:%d,column:%d\n",nrow, ncolumn);
	if(nrow == 0)
	{
		sprintf(buf, "select goods_count from goods where dev_no = '%d'", sto);
		DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);
		if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
		{
			DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
			return -2;
		}
		DDEBUG(P_URGENCY,"<<SQLIST>> row:%d,column:%d\n",nrow, ncolumn);
		
		
		sprintf(buf, "insert into goods values(%d,1,1,%d,%d,\"%s\",\"\")",sto, nrow+1, goodsnum, goodsvalue);
		DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);

		if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
		{
			DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
			return -2;
		}
		return 0;
	}


	if(cmd)
		sprintf(buf, "update  goods set goods_count=goods_count+1 where dev_no = '%d' and goodsvalue = \"%s\"", sto, goodsvalue);
	else if(atoi(resultp[ncolumn]) == 0)
		return 0;	
	else
		sprintf(buf, "update  goods set goods_count=goods_count-1 where dev_no = '%d' and goodsvalue = \"%s\"", sto, goodsvalue);
//		sprintf(P_NORMAL,buf, "select * from goods",goodsnum);

	DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);

	if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
		return -2;
	}
	


	return 0;
}

#else
int sql_ctl_goods(int goodsnum, int cmd)
{
	sqlite3 *db;
	if(sqlite3_open("/www/cgi-bin/warehouse.db",&db) != SQLITE_OK)
	{
		DDEBUG(P_URGENCY,"<<SQLIST>> error:%s\n",sqlite3_errmsg(db));
		return -1;
	}
	char buf[100] = {0};
	if(cmd)
		sprintf(buf, "update  goods set goods_count=goods_count+1 where goodsnum = \'%d\'",goodsnum);
	else
		sprintf(buf, "update  goods set goods_count=goods_count-1 where goodsnum = \'%d\'",goodsnum);
//		sprintf(buf, "select * from goods",goodsnum);

	DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);
	char *errmsg, **resultp;
	int nrow, ncolumn/*, i,j,index*/;

	if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
		return -2;
	}
	DDEBUG(P_URGENCY,"<<SQLIST>> row:%d,column:%d\n",nrow, ncolumn);


	return 0;
}


#endif
/*函数功能:向数据库中存放环境警戒线信息
 *参数：no-仓库号， *limit-环境警戒线结构体指针
 */
int sql_write_limit(int no, limit_t *limit)
{
	sqlite3 *db;
	if(sqlite3_open("/www/cgi-bin/warehouse.db",&db) != SQLITE_OK)
	{
		DDEBUG(P_URGENCY,"<<SQLIST>> error:%s\n",sqlite3_errmsg(db));
		return -1;
	}
	char buf[200] = {0};
	sprintf(buf, "update envlimit set tem_max=\'%d\',tem_min=\'%d\',hum_max=\'%d\',hum_min=\'%d\',light_max=\'%d\',light_min=\'%d\' where dev_no=\'%d\'",limit->tem_max.htem,limit->tem_min.htem,limit->hum_max.hhum,limit->hum_min.hhum,limit->light_max.light,limit->light_min.light,no);
	DDEBUG(P_URGENCY,"<<SQLIST>> WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW\n");
	DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);
	char *errmsg, **resultp;
	int nrow, ncolumn/*, i,j,index*/;

	if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
		return -2;
	}
	DDEBUG(P_URGENCY,"<<SQLIST>> row:%d,column:%d\n",nrow, ncolumn);

	return 0;
	
}

/*函数功能：从数据库中读取环境警戒线
 *参数：no-仓库号， *limit-环境警戒线结构体指针
 */
int sql_read_limit(int no, limit_t *limit)
{
	sqlite3 *db;
	if(sqlite3_open("/www/cgi-bin/warehouse.db",&db) != SQLITE_OK)
	{
		DDEBUG(P_URGENCY,"<<SQLIST>> error:%s\n",sqlite3_errmsg(db));
		return -1;
	}
	char buf[100] = {0};
	sprintf(buf, "select * from envlimit where dev_no = \'%d\'",no);
	DDEBUG(P_URGENCY,"<<SQLIST>> RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\n");
	DDEBUG(P_NORMAL,"<<SQLIST>> %s\n",buf);
	char *errmsg, **resultp;
	int nrow, ncolumn/*, i,j,index*/;

	if(sqlite3_get_table(db, buf, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		DDEBUG(P_NORMAL,"<<SQLIST>> error:%s\n",errmsg);
		return -2;
	}
	DDEBUG(P_URGENCY,"<<SQLIST>> row:%d,column:%d\n",nrow, ncolumn);
	if(nrow == 0)
		return -1;
	ncolumn++;
	limit->tem_max.htem = atoi(resultp[ncolumn++]);
	limit->tem_min.htem = atoi(resultp[ncolumn++]);
	limit->hum_max.hhum = atoi(resultp[ncolumn++]);
	limit->hum_min.hhum = atoi(resultp[ncolumn++]);
	limit->light_max.light = atoi(resultp[ncolumn++]);
	limit->light_min.light = atoi(resultp[ncolumn++]);

	return 0;
	

}



